package com.silent.week.week02;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liutao
 * Date 2021/3/29 7:42 下午
 * Description: http请求
 * Version: 1.0
 **/
public class HttpCallHandler {

    private final static Logger logger = LoggerFactory.getLogger(HttpCallHandler.class);
    private static HttpClient httpClient = new DefaultHttpClient();


    private static String getRequest(String url) throws Exception {
        // 创建一个HttpGet对象
        HttpGet get = new HttpGet(url);
        // 发送GET请求，返回响应
        HttpResponse httpResponse = httpClient.execute(get);
        // 如果服务器成功地返回响应
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            // 获取服务器响应字符串
            String result = EntityUtils.toString(httpResponse.getEntity());
            logger.info("GET 返回数据为: {}", result);
            return result;
        }
        return null;
    }

    public static void main(String[] args) throws Exception {

        getRequest("http://127.0.0.1:8808/test");
        getRequest("http://127.0.0.1:8808/callback");
    }


}
